package com.exercise.AndroidCamera;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
/*
기능 : 친구리스트 어탭터
작성 : 하드캔
완료일 : 2014.09.26 
	*/
public class FriendListAdapter extends BaseAdapter{
	Intent i;
	String userID;
	
	private Context context;
	private String friendId;
	private int friendCount, layoutId;
	private ArrayList<String> friendList;
	private LayoutInflater inflater;
	boolean[] itemChecked;
	private ViewHolder1 viewholder = null;
	Typeface tf;

	
	public FriendListAdapter(Context context, int layoutId, ArrayList<String> friendList, int friendCount, String userID) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutId = layoutId;
		this.friendList = friendList;
		this.friendCount = friendCount;
		tf = Typeface.createFromAsset(context.getAssets(), "fonts/whitechok.ttf");
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		itemChecked = new boolean[friendList.size()];
		this.userID = userID;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return friendList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return friendList.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	private class ViewHolder1 
	{
		TextView friendId = null;
		Button info;
		Button delete;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		// TODO Auto-generated method stub
		if(convertView == null) {
			viewholder = new ViewHolder1();
			convertView = inflater.inflate(layoutId, parent, false);
			viewholder.friendId = (TextView)convertView.findViewById(R.id.friendId);
			viewholder.friendId.setText(friendList.get(position));
			viewholder.friendId.setGravity(Gravity.CENTER);
			viewholder.friendId.setTypeface(tf);
			viewholder.friendId.setTextSize(20);
			viewholder.info = (Button)convertView.findViewById(R.id.info);
			viewholder.delete = (Button)convertView.findViewById(R.id.delete);
			convertView.setTag(viewholder);
			convertView.setBackgroundResource(R.drawable.round_background_reverse);
		}
		else
		{
			viewholder = (ViewHolder1)convertView.getTag();
		}
		
		viewholder.info.setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
				    	i = new Intent(context, FriendInfoActivity.class);
				    	Activity a = (Activity)context;
				    	i.putExtra("userID", userID);
				    	i.putExtra("friendID", friendList.get(position));
				    	a.startActivityForResult(i,3);
					}
				});	
		
		viewholder.delete.setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						String friendID = viewholder.friendId.getText().toString();
						friend_list_delete(friendList.get(position));
				    	i = new Intent(context, ManageFriendActivity.class);
				    	Activity a = (Activity)context;
				    	i.putExtra("userID", userID);
				    	i.putExtra("friendID", friendID);
				    	Log.d("friendID",friendID);
				    	a.startActivityForResult(i,3);
					}
				});

			
		return convertView;
	}
	
	protected String[] friend_list_delete(String friend_id){
		String pString = null;
		HttpURLConnection conn = null;
		BufferedReader br = null;
		
		String[] result = new String[2];
		result[0] = "error"; //JSON flag
		result[1] = null; //message
		
		try{
       	StringBuilder builder = new StringBuilder();
       	
       	URL url = new URL("http://54.199.194.197/delete_friend.php");
       	conn = (HttpURLConnection)url.openConnection();
       	
       	if(conn != null){
       		conn.setConnectTimeout(150000);	
       		conn.setReadTimeout(150000);
       		conn.setDefaultUseCaches(false); 
           	conn.setDoInput(true); 
           	conn.setDoOutput(true);  
           	conn.setRequestMethod("POST");
           	conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
              
         	StringBuffer buffer = new StringBuffer();
         	buffer.append("user_id").append("=").append(userID).append("&");
         	buffer.append("friend_id").append("=").append(friend_id);

            
            OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            PrintWriter writer = new PrintWriter(outStream);
            writer.write(buffer.toString());
            writer.flush();
            
            InputStreamReader inputStream = new InputStreamReader(conn.getInputStream(), "UTF-8"); 
            br = new BufferedReader(inputStream);
              
           	String line;
           	while((line = br.readLine()) != null){
           	   builder.append(line);
           	}
              
           	br.close();
           	
           	pString =  builder.toString();
           	Log.d("pString in read comment", pString);
           	
       	}else{
       		
       		throw new Exception("network error");
       	}
       	
       }catch(SocketTimeoutException se){
       	
       	result[1] = "network error";
       	
       }catch(Exception e){
       	
       	result[1] = e.getMessage();
       	
       }finally{
       	
       	if(conn != null)
       		conn.disconnect();

       	if(br != null){
       		try {
       			br.close();
				} catch (Exception e2) {
					// TODO: handle exception
					result[1] = e2.getMessage();
				}
       	}
       }
       return result;
	}

}
